const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],
    addressTotal:0,
    addressId: '',
    num:0,
    shopId:'',
    shop:{},
    addressName:'',
    phone:'',
    address:''
  },

  getShop(){
    var that = this;
    wx.request({
      url: httpUrl + '/shop/'+that.data.shopId,
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            shop:res.data.data
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  gotoInput(){
    wx.navigateTo({
      url: '/pages/input/input',
    })
  },

  getAddressList(){
    var that = this;
    wx.request({
      url: httpUrl + '/address',
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            addressList:res.data.data.rows,
            addressTotal:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  onChange(event) {
    this.setData({
      addressId: event.detail,
    });
  },
  buy(){
    if (this.data.addressId=='') {
      wx.showToast({
        title: '请选择地址',
        icon:'error'
      })
    }else{
      
      var that = this;
      wx.request({
        url: httpUrl + '/shop/count/' + that.data.shopId,
        method:'GET',
        header:{
          'Shop_token':wx.getStorageSync('Shop_token')
        },
        success(res){
          
          if (res.data.code == 20041) {
            if (res.data.data < that.data.num) {
              wx.showToast({
                title: '库存不足',
                icon:'none'
              })
              return;
            }else{
              wx.request({
                url: httpUrl + '/address/' + that.data.addressId,
                method:'GET',
                header:{
                  'Shop_token':wx.getStorageSync('Shop_token')
                },
                success(res){
                  if (res.data.code == 20041) {
                    that.setData({
                      addressName:res.data.data.name,
                      phone:res.data.data.phone,
                      address:res.data.data.address
                    },()=>{
                      var date = new Date().getTime();
                      wx.request({
                        url: httpUrl + '/order',
                        method:'POST',
                        header:{
                          'Shop_token':wx.getStorageSync('Shop_token')
                        },
                        data:{
                          count:that.data.num,
                          price:that.data.shop.price * that.data.num,
                          shopId:that.data.shopId,
                          addressName:that.data.addressName,
                          phone:that.data.phone,
                          address:that.data.address,
                          date:date
                        },
                        success(res){
                          if (res.data.code == 20011) {
                            wx.showToast({
                              title: '支付成功',
                              icon:'success'
                            });
                            setTimeout(function() {
                              wx.navigateBack({
                                delta: 1
                              });
                            }, 300);
                          }
                          else if (res.data.code == 401) {
                            console.log(res.data.msg);
                            wx.removeStorageSync('Shop_token');
                            wx.navigateTo({
                              url: '/pages/login/login',
                            })
                          }
                          else if (res.data.code == 402) {
                            console.log(res.data.msg);
                            // wx.removeStorageSync('Shop_token');
                            wx.navigateTo({
                              url: '/pages/login/login',
                            })
                          }
                          else{
                            console.log(res.data.msg);
                          }
                        },
                        fail(err){
                          console.log(err);
                        }
                      })
                    })
                  }
                  else if (res.data.code == 401) {
                    console.log(res.data.msg);
                    wx.removeStorageSync('Shop_token');
                    wx.navigateTo({
                      url: '/pages/login/login',
                    })
                  }
                  else if (res.data.code == 402) {
                    console.log(res.data.msg);
                    // wx.removeStorageSync('Shop_token');
                    wx.navigateTo({
                      url: '/pages/login/login',
                    })
                  }
                  else{
                    console.log(res.data.msg);
                  }
                },
                fail(err){
                  console.log(err);
                }
              })
            }
          }
          else if (res.data.code == 401) {
            console.log(res.data.msg);
            wx.removeStorageSync('Shop_token');
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
          else if (res.data.code == 402) {
            console.log(res.data.msg);
            // wx.removeStorageSync('Shop_token');
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
          else{
            console.log(res.data.msg);
          }
        },
        fail(err){
          console.log(err);
        }
      })


    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    this.getAddressList();
    this.setData({
      num:options.num,
      shopId:options.shopId
    },()=>{
      that.getShop();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})